package com.fast.cron.db.service;

import com.fast.cron.db.RowMapper;
import com.fast.cron.db.dao.UserDao;
import com.fast.cron.db.dao.AbstractDao;
import com.fast.cron.db.entry.User;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: Leo Jie
 * @date: 2018-10-02 20:51
 * @version: v1.0
 */
public class UserService extends AbstractDao implements UserDao {
    @Override
    public User findOneUserByUsername(String username) throws SQLException {
        String sql = "select user_id,username,email,role,`status` from fast_cron_user " +
                "where username = ? and `status` = 2";
        return queryOne(User.class, sql, new UserRowMapper().rowMapperMap(), username);
    }

    @Override
    public User findOneUserByUsernameAndPassword(String username, String password) throws SQLException {
        String sql = "select user_id,username,email,role,status from fast_cron_user " +
                "where username = ? and password = ? and status = 2";
        return queryOne(User.class, sql, new UserRowMapper().rowMapperMap(), username, password);

    }

    private class UserRowMapper implements RowMapper {
        @Override
        public Map rowMapperMap() {
            Map<String, String> rowMap = new HashMap<>(1);
            rowMap.put("user_id", "userId");
            return rowMap;
        }
    }

}
